
// 导入 SweetAlert2
import Swal from 'sweetalert2';

import axios from "axios";


export default {
    name: "StudentProgress",
    data() {
        return {
            studentInfo: [],
            searchValue: '',
        };
    },
    created: function() {
        this.fetchStudents();
    },
    methods: {
        //是否报名
        getSignUpStatus(status) {
            return status === 0 ? '未报名' : '已报名';
        },
        //科目按钮显示
        getButtonClasses(score, passingScore) {
            const parsedScore = parseInt(score, 10);

            return {
                btn: true,
                "btn-passed": parsedScore >= passingScore && parsedScore !== -1,
                "btn-failed": parsedScore >= 0 && parsedScore < passingScore,
                "btn-unattempted": parsedScore === -1,
            };
        },

        getDisplayedLabel(score) {
            const parsedScore = parseInt(score, 10);
            return parsedScore === -1 ? '未考试' : score;
        },
        //更新分数
        async handleButtonClick(studentId, subject) {
            const { value: newScore } = await Swal.fire({
                title: '输入新的成绩分数',
                input: 'number',
                inputValue: '',
                inputPlaceholder: '输入新的分数',
                showCancelButton: true,
                confirmButtonText: '更新分数',
                cancelButtonText: '取消',
                inputValidator: (value) => {
                    if (!value) {
                        return '请填写分数！';
                    }
                }
            });

            if (newScore) {
                this.updateScore(studentId, subject, parseInt(newScore, 10));
            }
        },
        //更新分数
        async updateScore(id, subject, newScore) {
            try {
                const url = `http://localhost:8081/student/updateScore/${id}/${subject}`;
                const response = await axios.put(url, {}, { params: { newScore } });
                if(response.data.code === 20000){
                    this.$message.success("更新成功！");
                    this.fetchStudents();
                }
                else{
                    this.$message.error(response.data.message);
                }
            } catch (error) {
                console.error("Updating score error:", error);
            }
        },
        // 显示学员信息
        async fetchStudents() {
            try {
                const url = "http://localhost:8081/student/list";
                const response = await axios.get(url);

                this.studentInfo = response.data.data.map(student => {
                    return {
                        ...student,
                        editing: false,
                    };
                });
            } catch (error) {
                console.error("Fetching students error:", error);
            }
        },



    },
};